

package biofilter.util;

import java.util.LinkedList;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 *
 * @author qvh
 */
public class SequencePositionTest {

    public SequencePositionTest() {
    }

    @BeforeClass
    public static void setUpClass() throws Exception {
    }

    @AfterClass
    public static void tearDownClass() throws Exception {
    }

    @Before
    public void setUp() {
    }

    @After
    public void tearDown() {
    }

    /**
     * Test of set method, of class SequencePosition.
     */
    @Test
    public void testSet_3args() {
        System.out.println("set");
        int s = 1;
        int e = 10;
        char stnd = '+';
        SequencePosition instance = new SequencePosition();
        instance.set(s, e, stnd);
        assertEquals(s, instance.getStart());
        assertEquals(e, instance.getEnd());
        assertEquals(stnd, instance.getStrand());
    }

    /**
     * Test of createEMBL method, of class SequencePosition.
     */
    @Test
    public void testCreateEMBL() {
        System.out.println("createEMBL");
        SequencePosition instance = new SequencePosition(1, 10, '+');
        SequencePosition instance2 = new SequencePosition(1, 10, '-');
        String expResult = "1..10";
        String expResult2 = "complement(1..10)";
        String result = instance.createEMBL();
        String result2 =  instance2.createEMBL();
        assertEquals(expResult, result);
        assertEquals(expResult2, result2);
    }

    /**
     * Test of setPositions method, of class SequencePosition.
     */
    @Test
    public void testSetPositions() {
        System.out.println("setPositions");
        String indxs = "";
        //Below is the useful tests for this...
        //LinkedList result = instance.setPositions(indxs, ll);
        //assertEquals(expResult, result);

    }

    /**
     * Test of set method, of class SequencePosition.
     */
    @Test
    public void testSet_String() {
        System.out.println("set");
        //standard
        String indxs = "100..200";
        SequencePosition instance = new SequencePosition();
        LinkedList ll = instance.set(indxs);
        SequencePosition p1 = (SequencePosition) ll.get(0);
        SequencePosition r1 = new SequencePosition(100,200,'+');
        assertEquals(p1.getStrand(), '+');
        assertEquals(p1.compareTo(r1), 0);
        //complement
        indxs = "complement(100..200)";
        ll = instance.set(indxs);
        p1 = (SequencePosition) ll.get(0);
        r1 = new SequencePosition(100,200,'-');
        assertEquals(p1.getStrand(), '-');
        assertEquals(p1.compareTo(r1), 0);
        indxs = "join(100..200)";
        ll = instance.set(indxs);
        p1 = (SequencePosition) ll.get(0);
        r1 = new SequencePosition(100,200,'-');
        assertEquals(p1.getStrand(), '+');
        assertEquals(p1.compareTo(r1), 0);
       //join
        indxs = "join(100..200,300..400)";
        ll = instance.set(indxs);
        p1 = (SequencePosition) ll.get(0);
        SequencePosition p2 = (SequencePosition) ll.get(1);
        r1 = new SequencePosition(100,200,'+');
        SequencePosition r2 = new SequencePosition(300,400,'+');
        assertEquals(p1.getStrand(), '+');
        assertEquals(p2.getStrand(), '+');
        assertEquals(p1.compareTo(r1), 0);
        assertEquals(p2.compareTo(r2), 0);
        //complement(join(1234..1256,2345..2367))
        indxs = "complement(join(100..200,300..400))";
        ll = instance.set(indxs);
        p1 = (SequencePosition) ll.get(0);
        p2 = (SequencePosition) ll.get(1);
        r1 = new SequencePosition(100,200,'-');
        r2 = new SequencePosition(300,400,'-');
        assertEquals(p1.getStrand(), '-');
        assertEquals(p2.getStrand(), '-');
        assertEquals(p1.compareTo(r1), 0);
        assertEquals(p2.compareTo(r2), 0);
        //complement(join(1234..1256,2345..2367))
        indxs = "complement(join(100..200,300..400,500..600))";
        ll = instance.set(indxs);
        p1 = (SequencePosition) ll.get(0);
        p2 = (SequencePosition) ll.get(1);
        SequencePosition p3 = (SequencePosition) ll.get(2);
        r1 = new SequencePosition(100,200,'-');
        r2 = new SequencePosition(300,400,'-');
        SequencePosition r3 = new SequencePosition(500,600,'-');
        assertEquals(p1.getStrand(), '-');
        assertEquals(p2.getStrand(), '-');
        assertEquals(p3.getStrand(), '-');
        assertEquals(p1.compareTo(r1), 0);
        assertEquals(p2.compareTo(r2), 0);
        assertEquals(p3.compareTo(r3), 0);

    }

    /**
     * Test of flipStartAndEnd method, of class SequencePosition.
     */
    @Test
    public void testFlipStartAndEnd() {
        System.out.println("flipStartAndEnd");
        SequencePosition instance = new SequencePosition(1,5,'+');
        instance.flipStartAndEnd();
        assertEquals(instance.getStrand(), '+');
        instance.flipStartAndEnd();
        assertEquals(instance.getStart(), 1);
        assertEquals(instance.getEnd(), 5);
        assertEquals(instance.getStrand(), '+');
    }

    /**
     * Test of flipStartEndStrand method, of class SequencePosition.
     */
    @Test
    public void testFlipStartEndStrand() {
        System.out.println("flipStartEndStrand");
        SequencePosition instance = new SequencePosition(1,5,'+');
        instance.flipStartEndStrand();
        assertEquals(instance.getStrand(), '-');
        instance.flipStartEndStrand();
        assertEquals(instance.getStart(), 1);
        assertEquals(instance.getEnd(), 5);
        assertEquals(instance.getStrand(), '+');
    }

    /**
     * Test of checkOrder method, of class SequencePosition.
     */
    @Test
    public void testCheckOrder() {
        System.out.println("checkOrder");
        SequencePosition instance = new SequencePosition(1,3,'+');
        SequencePosition instance2 = new SequencePosition(3,1,'+');  //Note it is actually not possible to break this because the constructor callse checkOrder
        assertEquals(instance.checkOrder(), 1);
    }

    /**
     * Test of compareTo method, of class SequencePosition.
     */
    @Test
    public void testCompareTo() {
        System.out.println("compareTo");
        Object o = null;
        SequencePosition instance = new SequencePosition(3,6,'+');
        SequencePosition instance2 = new SequencePosition(3,6,'+');
        SequencePosition instance3 = new SequencePosition(3,6,'-');
        SequencePosition instance4 = new SequencePosition(1,2,'+');
        SequencePosition instance5 = new SequencePosition(1,2,'-');
        SequencePosition instance6 = new SequencePosition(7,9,'+');
        SequencePosition instance7 = new SequencePosition(7,9,'-');
        SequencePosition instance8 = new SequencePosition(1,3,'+');
        SequencePosition instance9 = new SequencePosition(1,5,'+');
        SequencePosition instance10 = new SequencePosition(1,3,'-');
        SequencePosition instance11 = new SequencePosition(1,5,'-');
        SequencePosition instance12 = new SequencePosition(6,7,'+');
        SequencePosition instance13 = new SequencePosition(6,7,'-');
        SequencePosition instance14 = new SequencePosition(5,7,'+');
        SequencePosition instance15 = new SequencePosition(5,7,'-');

        int eq = 0;
        int lt = -1;
        int gt = 1;
        assertEquals(eq, instance.compareTo(instance2));
        assertEquals(eq, instance.compareTo(instance3));
        assertEquals(lt, instance.compareTo(instance4));
        assertEquals(lt, instance.compareTo(instance5));
        assertEquals(gt, instance.compareTo(instance6));
        assertEquals(gt, instance.compareTo(instance7));
        assertEquals(lt, instance.compareTo(instance8));
        assertEquals(lt, instance.compareTo(instance9));
        assertEquals(lt, instance.compareTo(instance10));
        assertEquals(lt, instance.compareTo(instance11));
        assertEquals(gt, instance.compareTo(instance12));
        assertEquals(gt, instance.compareTo(instance13));
        assertEquals(gt, instance.compareTo(instance14));
        assertEquals(gt, instance.compareTo(instance15));

    }

    /**
     * Test of deepCopy method, of class SequencePosition.
     */
    @Test
    public void testDeepCopy() {
        System.out.println("deepCopy");
        SequencePosition instance = new SequencePosition(2,6,'+');
        SequencePosition expResult = new SequencePosition(2,6,'+');
        SequencePosition result = instance.deepCopy();
        assertEquals(expResult.compareTo(result), 0);
    }

    /**
     * Test of length method, of class SequencePosition.
     */
    @Test
    public void testLength() {
        // xxATGAxx
        // 12345678
        System.out.println("length");
        SequencePosition instance = new SequencePosition(3, 6, '+');
        int expResult = 4;
        int result = instance.length();
        assertEquals(expResult, result);
    }

    /**
     * Test of overlaps method, of class SequencePosition.
     */
    @Test
    public void testOverlaps() {
        System.out.println("overlaps");
        SequencePosition instance = new SequencePosition(3,6,'+');
        SequencePosition instance2 = new SequencePosition(3,6,'+');
        SequencePosition instance3 = new SequencePosition(3,6,'-');
        SequencePosition instance4 = new SequencePosition(1,2,'+');
        SequencePosition instance5 = new SequencePosition(1,2,'-');
        SequencePosition instance6 = new SequencePosition(7,9,'+');
        SequencePosition instance7 = new SequencePosition(7,9,'-');
        SequencePosition instance8 = new SequencePosition(1,3,'+');
        SequencePosition instance9 = new SequencePosition(1,5,'+');
        SequencePosition instance10 = new SequencePosition(1,3,'-');
        SequencePosition instance11 = new SequencePosition(1,5,'-');
        SequencePosition instance12 = new SequencePosition(6,7,'+');
        SequencePosition instance13 = new SequencePosition(6,7,'-');
        SequencePosition instance14 = new SequencePosition(5,7,'+');
        SequencePosition instance15 = new SequencePosition(5,7,'-');


        assertEquals(true, instance.overlaps(instance2));
        assertEquals(true, instance.overlaps(instance3));
        assertEquals(false, instance.overlaps(instance4));
        assertEquals(false, instance.overlaps(instance5));
        assertEquals(false, instance.overlaps(instance6));
        assertEquals(false, instance.overlaps(instance7));
        assertEquals(true, instance.overlaps(instance8));
        assertEquals(true, instance.overlaps(instance9));
        assertEquals(true, instance.overlaps(instance10));
        assertEquals(true, instance.overlaps(instance11));
        assertEquals(true, instance.overlaps(instance12));
        assertEquals(true, instance.overlaps(instance13));
        assertEquals(true, instance.overlaps(instance14));
        assertEquals(true, instance.overlaps(instance15));

    }

    /**
     * Test of overlapNumber method, of class SequencePosition.
     */
    @Test
    public void testOverlapNumber() {
        System.out.println("overlapNumber");
        SequencePosition instance = new SequencePosition(3,6,'+');
        SequencePosition instance2 = new SequencePosition(3,6,'+');
        SequencePosition instance3 = new SequencePosition(3,6,'-');
        SequencePosition instance4 = new SequencePosition(1,2,'+');
        SequencePosition instance5 = new SequencePosition(1,2,'-');
        SequencePosition instance6 = new SequencePosition(7,9,'+');
        SequencePosition instance7 = new SequencePosition(7,9,'-');
        SequencePosition instance8 = new SequencePosition(1,3,'+');
        SequencePosition instance9 = new SequencePosition(1,5,'+');
        SequencePosition instance10 = new SequencePosition(1,3,'-');
        SequencePosition instance11 = new SequencePosition(1,5,'-');
        SequencePosition instance12 = new SequencePosition(6,7,'+');
        SequencePosition instance13 = new SequencePosition(6,7,'-');
        SequencePosition instance14 = new SequencePosition(5,7,'+');
        SequencePosition instance15 = new SequencePosition(5,7,'-');


        assertEquals(4, instance.overlapNumber(instance2));
        assertEquals(4, instance.overlapNumber(instance3));
        assertEquals(0, instance.overlapNumber(instance4));
        assertEquals(0, instance.overlapNumber(instance5));
        assertEquals(0, instance.overlapNumber(instance6));
        assertEquals(0, instance.overlapNumber(instance7));
        assertEquals(1, instance.overlapNumber(instance8));
        assertEquals(3, instance.overlapNumber(instance9));
        assertEquals(1, instance.overlapNumber(instance10));
        assertEquals(3, instance.overlapNumber(instance11));
        assertEquals(1, instance.overlapNumber(instance12));
        assertEquals(1, instance.overlapNumber(instance13));
        assertEquals(2, instance.overlapNumber(instance14));
        assertEquals(2, instance.overlapNumber(instance15));
    }

}